package com.system.travel.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.system.travel.modules.system.entity.SysRole;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 系统角色Mapper接口
 *
 * @author lucas
 * @date 2024/01/01
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

    /**
     * 通过用户ID查询角色
     */
    @Select("SELECT r.* FROM sys_role r " +
            "LEFT JOIN sys_user_role ur ON r.id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.status = 1 AND r.del_flag = 0")
    List<SysRole> selectRolesByUserId(Long userId);

    /**
     * 通过角色编码查询角色
     */
    @Select("select * from sys_role where role_code = #{roleCode} and del_flag = 0")
    SysRole selectByRoleCode(@Param("roleCode") String roleCode);

    /**
     * 删除角色权限关联
     */
    @Delete("delete from sys_role_permission where role_id = #{roleId}")
    int deleteRolePermissions(@Param("roleId") Long roleId);

    /**
     * 批量插入角色权限关联
     */
    @Insert("<script>" +
            "insert into sys_role_permission(role_id, permission_id) values " +
            "<foreach collection='permissionIds' item='permissionId' separator=','>" +
            "(#{roleId}, #{permissionId})" +
            "</foreach>" +
            "</script>")
    int insertRolePermissions(@Param("roleId") Long roleId, @Param("permissionIds") List<Long> permissionIds);

    /**
     * 查询角色的权限ID列表
     */
    @Select("select permission_id from sys_role_permission where role_id = #{roleId}")
    List<Long> selectPermissionIds(@Param("roleId") Long roleId);
} 